package com.wkky.mechanismscreenings.controller;


import com.wkky.mechanismscreenings.dtovo.StaffDTO;
import com.wkky.mechanismscreenings.dtovo.StaffPageQueryDTO;
import com.wkky.mechanismscreenings.results.PageResult;
import com.wkky.mechanismscreenings.results.Result;
import com.wkky.mechanismscreenings.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/excel")
@CrossOrigin
@Slf4j
public class StaffController {

    @Autowired
    private StaffService staffService;

    /**
     * 分页查询
     * @param staffPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(StaffPageQueryDTO staffPageQueryDTO){
        log.info("分页查询的参数为：{}",staffPageQueryDTO);
        PageResult pageResult = staffService.pageQuery(staffPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
     * 筛选数据
     * @param staffPageQueryDTO
     * @return
     */
    @GetMapping("/screenout")
    public Result<PageResult> screenOutQuery(StaffPageQueryDTO staffPageQueryDTO){
        PageResult pageResult = staffService.screenOutQuery(staffPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/exprotSelectedStaff")
    public Result exportSelectedStaff(@RequestParam List<String> ids,HttpServletResponse response){
        staffService.downloadSelectedStaff(ids,response);
        return Result.success();
    }

    /**
     * 新增员工
     * @param staffDTO
     * @return
     */
    @PostMapping("/add")
    public Result save(@RequestBody StaffDTO staffDTO){
        staffService.save(staffDTO);
        return Result.success();

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result delBatch(@RequestParam List<String> ids){
        staffService.delete(ids);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param file
     * @return
     */



    @PostMapping("/staffImport")
    public String excelImport(@RequestPart("file") MultipartFile file) {

        staffService.importExcel(file);
        return "保存成功";

    }

    @GetMapping("/excelDownload")
    public String excelDownload(HttpServletResponse response,@RequestParam(defaultValue = "500") Integer limit) {
        //从数据库提取用户需要的数据，我这里是把所有数据导出来
       /* List<Staff> students = staffService.selectAllStudents();*/
        //尝试把随机导出十条数据
        //List<Staff> staffLists = staffService.getRandStaffs(limit,response);
        staffService.downloadStaffs(limit,response);
        return "导出成功";
    }
}
